<!-- eslint-disable no-console -->
<!-- 质量管理模块 - 过程检验 -->
<script lang="ts" setup>
import type { VbenFormProps } from '#/adapter/form';
import type { VxeGridListeners, VxeGridProps } from '#/adapter/vxe-table';
import type { DictApi, IpqcApi } from '#/api';

import { reactive } from 'vue';

import { useVbenModal } from '@vben/common-ui';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { DictResp, getIpqcList } from '#/api';

import delModal from './del-modal.vue';
import editModal from './edit-modal.vue';
// import {} from 'element-plus';
// 将字典数据添加到数组中;
const mes_qc_result = reactive<DictApi.SelectOption[]>([]);
const mes_order_status = reactive<DictApi.SelectOption[]>([]);
DictResp(['mes_qc_result', 'mes_order_status']).then((res) => {
  // 将两个数组合并
  if (res.mes_qc_result) {
    mes_qc_result.push(...res.mes_qc_result);
    // const mesindex = mes_index_type.map((item) => ({ value: item.value }));
    // console.log(mesindex);
  }
  if (res.mes_order_status) {
    mes_order_status.push(...res.mes_order_status);
  }
});

const [EditModal, editmodalApi] = useVbenModal({
  // 连接抽离的组件
  connectedComponent: editModal,
  showConfirmButton: false,
  showCancelButton: false,
});
const [DelModal, delmodalApi] = useVbenModal({
  // 连接抽离的组件
  connectedComponent: delModal,
});

const formOptions: VbenFormProps = {
  // 默认展开
  collapsed: false,
  schema: [
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入检验单编号',
      },
      fieldName: 'ipqcCode',
      label: '检验单编号',
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: mes_qc_result,
        placeholder: '请选择检验类型',
      },
      fieldName: 'ipqcType',
      label: '检验类型',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入工单编号',
      },
      fieldName: 'workorderCode',
      label: '工单编号',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入产品物料编码',
      },
      fieldName: 'itemCode',
      label: '产品物料编码',
    },
    {
      component: 'Input',
      componentProps: {
        placeholder: '请输入产品物料名称',
      },
      fieldName: 'itemName',
      label: '产品物料名称',
    },
    {
      component: 'Select',
      componentProps: {
        allowClear: true,
        options: mes_qc_result,
        placeholder: '请选择检测结果',
      },
      fieldName: 'checkResult',
      label: '检测结果',
    },
  ],
  // 控制表单是否显示折叠按钮
  showCollapseButton: false,
  submitButtonOptions: {
    content: '搜索',
  },
  // 是否在字段值改变时提交表单
  submitOnChange: false,
  // 按下回车时是否提交表单
  submitOnEnter: false,
};

const gridOptions = reactive<VxeGridProps<IpqcApi.Row>>({
  checkboxConfig: {
    highlight: true,
    labelField: 'name',
  },
  columns: [
    {
      align: 'left',
      field: 'iqcId',
      title: '',
      type: 'checkbox',
      width: 50,
    },
    { field: 'ipqcCode', title: '检验单编号' },
    { field: 'ipqcType', title: '检验类型' },
    { field: 'workorderCode', title: '工单编号' },
    { field: 'itemCode', title: '产品物料编码' },
    { field: 'itemName', title: '产品物料名称' },
    { field: 'specification', title: '规格型号' },
    { field: 'unitName', title: '单位' },
    { field: 'quantityCheck', title: '检测数量' },
    {
      field: 'checkResult',
      title: '检测结果',
      formatter: ({ cellValue }) => {
        const item = mes_qc_result.find((item) => item.value === cellValue);
        return item?.label || '';
      },
    },
    { field: 'inspectDate', title: '检测日期' },
    { field: 'inspector', title: '检测人员' },
    {
      field: 'status',
      title: '单据状态',
      formatter: ({ cellValue }) => {
        const item = mes_order_status.find((item) => item.value === cellValue);
        return item?.label || '';
      },
    },
    { slots: { default: 'action' }, title: '操作' },
  ],
  keepSource: true,
  pagerConfig: {},
  proxyConfig: {
    ajax: {
      query: async ({ page }) => {
        const res = await getIpqcList({
          pageNum: page.currentPage,
          pageSize: page.pageSize,
        });
        return {
          total: res.total,
          items: res.rows,
        };
      },
    },
  },

  toolbarConfig: {
    // 是否显示搜索表单控制按钮
    // @ts-ignore 正式环境时有完整的类型声明
    search: true,
    refresh: true,
    buttons: [
      {
        name: '新增',
        code: 'myAdd',
        status: 'primary',
        icon: 'vxe-icon-add',
      },
      {
        name: '修改',
        code: 'mySave',
        status: 'success',
        disabled: true,
        icon: 'vxe-icon-save',
      },
      {
        name: '删除',
        code: 'myDel',
        status: 'error',
        disabled: true,
        icon: 'vxe-icon-delete',
      },
    ],
  },
});

const gridEvents: VxeGridListeners = {
  toolbarButtonClick({ code }) {
    console.log(code);
    switch (code) {
      case 'myAdd': {
        // 新增逻辑
        console.log('新增');
        editmodalApi
          .setState({
            title: '新增检测项',
          })
          .setData({})
          .open();
        break;
      }
      case 'myDel': {
        // 删除逻辑
        console.log('删除');
        break;
      }
      case 'mySave': {
        // 保存逻辑
        console.log('保存');
        break;
      }
      default: {
        break;
      }
    }
  },
  checkboxChange({ row }) {
    console.log(row);
  },
};

const [Grid, gridApi] = useVbenVxeGrid({
  formOptions,
  gridOptions,
  gridEvents,
});

function editRowEvent(row: IpqcApi.Row) {
  // console.log(row);
  editmodalApi
    .setState({
      title: '修改检测项',
    })
    .setData({ row })
    .open();
  gridApi.grid?.setEditRow(row);
}
function deleteRowEvent(row: IpqcApi.Row) {
  console.log(row.ipqcId);
  // console.log(row);
  delmodalApi
    .setData(`是否确认删除检测项编号为"${row.ipqcId}"的数据项？`)
    .open();
  gridApi.grid?.setEditRow(row);
}
</script>

<template>
  <div class="vp-raw w-full">
    <EditModal />
    <DelModal />
    <Grid>
      <template #action="{ row }">
        <VxeButton
          mode="text"
          icon="vxe-icon-signature"
          @click="editRowEvent(row)"
          status="primary"
        >
          修改
        </VxeButton>
        <VxeButton
          icon="vxe-icon-delete"
          mode="text"
          @click="deleteRowEvent(row)"
          status="error"
        >
          删除
        </VxeButton>
      </template>
    </Grid>
  </div>
</template>
